﻿#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <windows.h>
#include<vector>
#include<ctime>
#include <mmsystem.h>  // 添加多媒体头文件
#pragma comment(lib, "winmm.lib")  // 链接多媒体库
using namespace std;

const int PLAYER_SPEED = 5;
const int MAX_BULLETS = 30;      // 增加子弹上限以支持多列
const int MAX_ENEMIES = 10;
const int MAX_EXPLOSIONS = 10;
const int MAX_POWER_UPS = 5;     // 最大道具数量
const int WIDTH = 480;
const int HEIGHT = 700;

// 游戏变量
int playerX, playerY;
int playerWidth, playerHeight;
int bulletWidth, bulletHeight;
int enemyWidth, enemyHeight;
int bulletTimer = 0;
int score = 0;
int lives = 3;                 // 玩家初始生命值
int scoreToAddLife = 200;      // 加分阈值
int currentScoreForLife = 0;   // 当前累计分数
int maxLives = 5;              // 最大生命值
bool gameOver = false;
bool playerDying = false;
int playerDeathFrame = 0;
bool lifeAdded = false;       // 生命增加提示标志
int lifeAddedTimer = 0;       // 提示计时器
bool playerInvincible = false; // 玩家无敌状态
int invincibleTimer = 0;       // 无敌计时器
bool boughtInvincible = false; // 购买的无敌状态标志
int boughtInvincibleTimer = 0; // 购买的无敌计时器
const int INVINCIBLE_COST = 80; // 无敌状态花费分数
const int INVINCIBLE_DURATION = 50; // 无敌状态持续时间(帧)

// 音乐播放状态
bool musicPlaying = false;     // 音乐播放状态
bool musicInitialized = false; // 音乐初始化状态

// 道具类型
enum PowerUpType {
    POWER_UP_NONE,
    POWER_UP_BOMB,     // 炸弹补给 - 加快攻击频率
    POWER_UP_BULLET    // 子弹补给 - 多发射子弹
};

// 道具结构体
struct PowerUp {
    int x, y;
    bool active;
    int type;
    int width, height;
};

// 游戏对象结构体
struct Enemy {
    int x, y;
    bool alive;
    int speed;
    int type;
    int hp;
    int width;
    int height;
};

struct Bullet {
    int x;
    int y;
    bool alive;
};

struct Explosion {
    int x;
    int y;
    int frame;
    bool active;
    int type;
};

// 游戏对象数组
vector<Bullet> bullets(MAX_BULLETS);
vector<Explosion> explosions(MAX_EXPLOSIONS);
vector<Enemy> enemies(MAX_ENEMIES);
vector<PowerUp> powerUps(MAX_POWER_UPS); // 道具数组
int enemySpawnTimer = 0;                 // 敌机生成计时器
int powerUpSpawnTimer = 0;               // 道具生成计时器

// 道具效果状态
bool bombEffectActive = false;  // 炸弹效果激活状态
int bombEffectTimer = 0;       // 炸弹效果计时器
int bulletColumns = 1;         // 子弹列数，初始1列
int maxBulletColumns = 4;      // 最大子弹列数
int bulletEffectTimer = 0;     // 子弹效果计时器

enum EnemyType {
    ENEMY_SMALL,
    ENEMY_MIDDLE,
    ENEMY_BIG,
};

// 图像资源
IMAGE img_background;
IMAGE img_player;
IMAGE img_bullet;
IMAGE img_enemy_small, img_enemy_middle, img_enemy_big;
IMAGE img_explosion_small[4];
IMAGE img_explosion_middle[4];
IMAGE img_explosion_big[6];
IMAGE img_player_death[4];
IMAGE img_bomb_supply, img_bullet_supply;

// 函数声明
void initGame();
void loadResources();
void processInput();
void updateGame();
void renderGame();
void checkBoundaries();
void drawAlpha(IMAGE* picture, int picture_x, int picture_y);
void initBullets();
void initEnemies();
void spawnEnemy();
void updateEnemies();
void checkCollisions();
void updateExplosions();
void checkPlayerHit();
void spawnExplosion(int x, int y, int type);
void checkAddLife();  // 检查是否增加生命
void showLifeAddedMessage();  // 显示生命增加提示
void showBoughtInvincibleMessage(); // 显示购买无敌状态提示
void spawnPowerUp();              // 生成道具
void playBackgroundMusic();       // 播放背景音乐
void stopBackgroundMusic();       // 停止背景音乐

int main()
{
    srand((unsigned)time(NULL));
    initGame();
    loadResources();
    BeginBatchDraw();

    while (true)
    {
        processInput();
        updateGame();
        checkBoundaries();
        renderGame();
        checkPlayerHit();
        checkCollisions();

        Sleep(20);
        if (GetAsyncKeyState(VK_ESCAPE))
            break;
    }

    EndBatchDraw();
    closegraph();
    stopBackgroundMusic(); // 程序结束时停止音乐
    return 0;
}

// 播放背景音乐
void playBackgroundMusic() {
    if (musicInitialized && !musicPlaying) {
        mciSendString("play LuckyKPoo -LuckyKPoo_The_Last_Radiance.mp3", NULL, 0, NULL);
        musicPlaying = true;
    }
}

// 停止背景音乐
void stopBackgroundMusic() {
    mciSendString("stop LuckyKPoo -LuckyKPoo_The_Last_Radiance.mp3", NULL, 0, NULL);
    mciSendString("close LuckyKPoo -LuckyKPoo_The_Last_Radiance.mp3", NULL, 0, NULL);
    musicPlaying = false;
    musicInitialized = false;
}

void initGame()
{
    initgraph(WIDTH, HEIGHT);
    playerX = WIDTH / 2 - 102 / 2;
    playerY = HEIGHT - 126;
    initBullets();
    initEnemies();

    // 初始化生命值和复活相关状态
    lives = 3;
    currentScoreForLife = 0;
    lifeAdded = false;
    lifeAddedTimer = 0;
    playerInvincible = false;
    invincibleTimer = 0;
    boughtInvincible = false;
    boughtInvincibleTimer = 0;

    // 初始化道具系统
    for (int i = 0; i < powerUps.size(); i++) {
        powerUps[i].active = false;
    }
    powerUpSpawnTimer = 250; // 初始等待5秒后生成第一个道具
    bombEffectActive = false;
    bulletColumns = 1;
    bulletEffectTimer = 0;

    // 初始化音乐系统
    mciSendString("open LuckyKPoo -LuckyKPoo_The_Last_Radiance.mp3", NULL, 0, NULL);
    musicInitialized = true;
    playBackgroundMusic(); // 开始播放音乐

    for (int i = 0; i < MAX_EXPLOSIONS; i++) {
        explosions[i].active = false;
    }
}

void initBullets() {
    for (int i = 0; i < MAX_BULLETS; i++) {
        bullets[i].alive = false;
        bullets[i].x = -100;
        bullets[i].y = -100;
    }
}

void loadResources()
{
    loadimage(&img_background, "../images/background.PNG", WIDTH, HEIGHT);
    loadimage(&img_player, "../images/me.PNG", 0, 0);
    playerWidth = img_player.getwidth();
    playerHeight = img_player.getheight();
    loadimage(&img_bullet, "../images/bullet.PNG");
    bulletWidth = img_bullet.getwidth();
    bulletHeight = img_bullet.getheight();
    loadimage(&img_enemy_small, "../images/enemy1.PNG");
    enemyWidth = img_enemy_small.getwidth();
    enemyHeight = img_enemy_small.getheight();
    loadimage(&img_enemy_middle, "../images/enemy2.PNG");
    enemyWidth = img_enemy_middle.getwidth();
    enemyHeight = img_enemy_middle.getheight();
    loadimage(&img_enemy_big, "../images/enemy3.PNG");
    enemyWidth = img_enemy_big.getwidth();
    enemyHeight = img_enemy_big.getheight();

    loadimage(&img_explosion_small[0], "../images/enemy1_down1.PNG");
    loadimage(&img_explosion_small[1], "../images/enemy1_down2.PNG");
    loadimage(&img_explosion_small[2], "../images/enemy1_down3.PNG");
    loadimage(&img_explosion_small[3], "../images/enemy1_down4.PNG");

    loadimage(&img_explosion_middle[0], "../images/enemy2_down1.PNG");
    loadimage(&img_explosion_middle[1], "../images/enemy2_down2.PNG");
    loadimage(&img_explosion_middle[2], "../images/enemy2_down3.PNG");
    loadimage(&img_explosion_middle[3], "../images/enemy2_down4.PNG");

    loadimage(&img_explosion_big[0], "../images/enemy3_down1.PNG");
    loadimage(&img_explosion_big[1], "../images/enemy3_down2.PNG");
    loadimage(&img_explosion_big[2], "../images/enemy3_down3.PNG");
    loadimage(&img_explosion_big[3], "../images/enemy3_down4.PNG");
    loadimage(&img_explosion_big[4], "../images/enemy3_down5.PNG");
    loadimage(&img_explosion_big[5], "../images/enemy3_down6.PNG");

    loadimage(&img_player_death[0], "../images/me_destroy_1.PNG");
    loadimage(&img_player_death[1], "../images/me_destroy_2.PNG");
    loadimage(&img_player_death[2], "../images/me_destroy_3.PNG");
    loadimage(&img_player_death[3], "../images/me_destroy_4.PNG");

    // 加载道具图片
    loadimage(&img_bomb_supply, "../images/bomb_supply.PNG");
    loadimage(&img_bullet_supply, "../images/bullet_supply.PNG");

    // 设置道具尺寸
    for (int i = 0; i < powerUps.size(); i++) {
        powerUps[i].width = img_bomb_supply.getwidth();
        powerUps[i].height = img_bomb_supply.getheight();
    }
}

void processInput()
{
    if (gameOver) {
        if (GetAsyncKeyState('R') & 0x8000) {
            gameOver = false;
            playerDying = false;
            playerDeathFrame = 0;
            playerInvincible = false;
            invincibleTimer = 0;
            boughtInvincible = false;
            boughtInvincibleTimer = 0;
            score = 0;
            playerX = WIDTH / 2 - 40;
            playerY = HEIGHT - 100;

            for (int i = 0; i < MAX_ENEMIES; i++) {
                enemies[i].alive = false;
            }

            for (int i = 0; i < MAX_BULLETS; i++) {
                bullets[i].alive = false;
            }

            for (int i = 0; i < MAX_EXPLOSIONS; i++) {
                explosions[i].active = false;
            }

            // 重置道具和效果
            for (int i = 0; i < powerUps.size(); i++) {
                powerUps[i].active = false;
            }
            bombEffectActive = false;
            bulletColumns = 1;
            bulletEffectTimer = 0;

            // 重置生命值
            lives = 3;
            currentScoreForLife = 0;
        }
        return;
    }

    // 处理购买无敌状态
    if (GetAsyncKeyState('X') & 0x8000 && score >= INVINCIBLE_COST && !boughtInvincible && !playerInvincible) {
        score -= INVINCIBLE_COST;
        boughtInvincible = true;
        boughtInvincibleTimer = INVINCIBLE_DURATION;
        printf("花费80分购买无敌状态!\n");
    }

    if (playerInvincible && !boughtInvincible) return; // 复活无敌状态不处理输入移动

    if (GetAsyncKeyState(VK_LEFT)) playerX -= PLAYER_SPEED;
    if (GetAsyncKeyState(VK_RIGHT)) playerX += PLAYER_SPEED;
    if (GetAsyncKeyState(VK_UP)) playerY -= PLAYER_SPEED;
    if (GetAsyncKeyState(VK_DOWN)) playerY += PLAYER_SPEED;

    if (GetAsyncKeyState(VK_SPACE) && bulletTimer == 0 && !(playerInvincible && !boughtInvincible)) {
        int bulletOffset = bulletWidth + 10; // 子弹间距
        int startX = playerX + (playerWidth / 2) - (bulletWidth * bulletColumns + bulletOffset * (bulletColumns - 1)) / 2;

        for (int col = 0; col < bulletColumns; col++) {
            for (int i = 0; i < MAX_BULLETS; i++) {
                if (!bullets[i].alive) {
                    bullets[i].x = startX + col * (bulletWidth + bulletOffset);
                    bullets[i].y = playerY - bulletHeight;
                    bullets[i].alive = true;
                    break;
                }
            }
        }

        // 根据是否激活炸弹效果调整冷却时间
        if (bombEffectActive) {
            bulletTimer = 5; // 加速攻击频率
        }
        else {
            bulletTimer = 10; // 正常攻击频率
        }
    }

    if (bulletTimer > 0) bulletTimer--;
}

void checkBoundaries()
{
    if (playerX < 0)
        playerX = 0;
    if (playerX > WIDTH - 102)
        playerX = WIDTH - 102;
    if (playerY < 0)
        playerY = 0;
    if (playerY > HEIGHT - 126)
        playerY = HEIGHT - 126;
}

const int BULLET_SPEED = 10;

void updateGame()
{
    for (int i = 0; i < MAX_BULLETS; i++) {
        if (bullets[i].alive) {
            bullets[i].y -= BULLET_SPEED;
            if (bullets[i].y < -100)
                bullets[i].alive = false;
        }
    }
    updateEnemies();
    updateExplosions();

    // 处理玩家死亡状态
    if (playerDying) {
        playerDeathFrame++;
        if (playerDeathFrame > 15) {
            // 玩家复活
            playerDying = false;
            playerInvincible = true;
            invincibleTimer = 60; // 无敌时间60帧（约1秒）
            playerX = WIDTH / 2 - playerWidth / 2;
            playerY = HEIGHT - playerHeight;
        }
    }

    // 处理购买的无敌状态
    if (boughtInvincible) {
        boughtInvincibleTimer--;
        if (boughtInvincibleTimer <= 0) {
            boughtInvincible = false;
        }
    }

    // 处理无敌状态
    if (playerInvincible && !boughtInvincible) {
        invincibleTimer--;
        if (invincibleTimer <= 0) {
            playerInvincible = false;
        }
    }

    // 组合无敌状态：复活无敌或购买无敌
    playerInvincible = playerInvincible || boughtInvincible;

    // 处理生命增加提示计时
    if (lifeAdded) {
        lifeAddedTimer++;
        if (lifeAddedTimer > 60) {
            lifeAdded = false;
            lifeAddedTimer = 0;
        }
    }

    // 更新道具效果
    if (bombEffectActive) {
        bombEffectTimer--;
        if (bombEffectTimer <= 0) {
            bombEffectActive = false;
        }
    }

    if (bulletEffectTimer > 0) {
        bulletEffectTimer--;
        if (bulletEffectTimer <= 0 && bulletColumns > 1) {
            bulletColumns--;
        }
    }

    // 更新道具位置
    for (int i = 0; i < powerUps.size(); i++) {
        if (powerUps[i].active) {
            powerUps[i].y += 2; // 道具下落速度
            if (powerUps[i].y > HEIGHT) {
                powerUps[i].active = false;
            }
        }
    }
}

void renderGame()
{
    putimage(0, 0, &img_background);

    // 绘制玩家飞机（考虑无敌状态闪烁效果）
    if (!playerDying) {
        if (!playerInvincible || (invincibleTimer / 10 % 2 == 0 && !boughtInvincible) ||
            (boughtInvincible && boughtInvincibleTimer / 10 % 2 == 0)) { // 无敌状态闪烁效果
            drawAlpha(&img_player, playerX, playerY);
        }
    }
    else if (playerDeathFrame < 12) {
        int frame = playerDeathFrame / 3;
        if (frame > 3) frame = 3;
        drawAlpha(&img_player_death[frame], playerX, playerY);
    }

    for (int i = 0; i < MAX_BULLETS; i++) {
        if (bullets[i].alive) {
            drawAlpha(&img_bullet, bullets[i].x, bullets[i].y);
        }
    }

    for (int i = 0; i < MAX_ENEMIES; i++) {
        if (enemies[i].alive) {
            if (enemies[i].type == ENEMY_SMALL) {
                drawAlpha(&img_enemy_small, enemies[i].x, enemies[i].y);
            }
            else if (enemies[i].type == ENEMY_MIDDLE) {
                drawAlpha(&img_enemy_middle, enemies[i].x, enemies[i].y);
            }
            else if (enemies[i].type == ENEMY_BIG) {
                drawAlpha(&img_enemy_big, enemies[i].x, enemies[i].y);
            }
            char hpInfo[20];
            sprintf_s(hpInfo, "HP:%d", enemies[i].hp);
            outtextxy(enemies[i].x, enemies[i].y - 15, hpInfo);
        }
    }

    // 渲染道具
    for (int i = 0; i < powerUps.size(); i++) {
        if (powerUps[i].active) {
            if (powerUps[i].type == POWER_UP_BOMB) {
                drawAlpha(&img_bomb_supply, powerUps[i].x, powerUps[i].y);
            }
            else if (powerUps[i].type == POWER_UP_BULLET) {
                drawAlpha(&img_bullet_supply, powerUps[i].x, powerUps[i].y);
            }
        }
    }

    for (int i = 0; i < MAX_EXPLOSIONS; i++) {
        if (explosions[i].active) {
            int frame = explosions[i].frame / 3;
            IMAGE* img = NULL;
            int imgWeight, imgHeight;

            switch (explosions[i].type) {
            case ENEMY_SMALL:
                if (frame < 4) {
                    img = &img_explosion_small[frame];
                    imgWeight = img->getwidth();
                    imgHeight = img->getheight();
                }
                break;
            case ENEMY_MIDDLE:
                if (frame < 4) {
                    img = &img_explosion_middle[frame];
                    imgWeight = img->getwidth();
                    imgHeight = img->getheight();
                }
                break;
            case ENEMY_BIG:
                if (frame < 6) {
                    img = &img_explosion_big[frame];
                    imgWeight = img->getwidth();
                    imgHeight = img->getheight();
                }
                break;
            }

            if (img) {
                drawAlpha(img, explosions[i].x - imgWeight / 2, explosions[i].y - imgHeight / 2);
            }
        }
    }

    if (gameOver) {
        outtextxy(WIDTH / 2 - 80, HEIGHT / 2, "Game Over,按R键重新开始");
    }

    setbkmode(TRANSPARENT);
    char positionInfo[50];
    sprintf_s(positionInfo, "位置: (%d, %d)", playerX, playerY);
    outtextxy(10, 10, positionInfo);

    int activeBullets = 0;
    for (int i = 0; i < MAX_BULLETS; i++) {
        if (bullets[i].alive) activeBullets++;
    }
    char bulletInfo[50];
    sprintf_s(bulletInfo, "子弹: %d/%d", activeBullets, MAX_BULLETS);
    outtextxy(10, 30, bulletInfo);

    int activeEnemies = 0;
    for (int i = 0; i < MAX_ENEMIES; i++) {
        if (enemies[i].alive) activeEnemies++;
    }
    char enemiesInfo[50];
    sprintf_s(enemiesInfo, "敌机: %d/%d", activeEnemies, MAX_ENEMIES);
    outtextxy(10, 45, enemiesInfo);

    char scoreInfo[50];
    sprintf_s(scoreInfo, "分数: %d", score);
    outtextxy(10, 70, scoreInfo);

    char livesInfo[50];
    sprintf_s(livesInfo, "生命: %d", lives);
    outtextxy(10, 90, livesInfo);

    // 显示无敌状态提示
    if (playerInvincible) {
        settextcolor(RED);
        if (boughtInvincible) {
            outtextxy(WIDTH / 2 - 60, HEIGHT / 2 - 30, "购买无敌状态!");
            char timeInfo[30];
            sprintf_s(timeInfo, "剩余: %d秒", boughtInvincibleTimer / 30);
            outtextxy(WIDTH / 2 - 30, HEIGHT / 2, timeInfo);
        }
        else {
            outtextxy(WIDTH / 2 - 40, HEIGHT / 2 - 30, "无敌状态!");
        }
        settextcolor(WHITE);
    }

    // 显示道具效果提示
    if (bombEffectActive) {
        settextcolor(YELLOW);
        outtextxy(WIDTH / 2 - 40, HEIGHT / 2 + 20, "攻击加速中!");
        settextcolor(WHITE);
    }

    if (bulletEffectTimer > 0 && bulletColumns > 1) {
        settextcolor(CYAN);
        sprintf_s(bulletInfo, "子弹列数: %d", bulletColumns);
        outtextxy(WIDTH / 2 - 40, HEIGHT / 2 + 20, bulletInfo);
        settextcolor(WHITE);
    }

    // 显示生命增加提示
    if (lifeAdded) {
        settextcolor(GREEN);
        outtextxy(WIDTH / 2 - 40, HEIGHT / 2 - 50, "获得额外生命!");
        settextcolor(WHITE);
    }

    // 显示操作提示
    outtextxy(10, 60, "方向键移动，空格发射子弹，X键购买无敌(80分)，ESC退出");

    FlushBatchDraw();
}

void drawAlpha(IMAGE* picture, int picture_x, int picture_y)
{
    DWORD* dst = GetImageBuffer();
    DWORD* draw = GetImageBuffer();
    DWORD* src = GetImageBuffer(picture);

    int picture_width = picture->getwidth();
    int picture_height = picture->getheight();
    int graphWidth = getwidth();
    int graphHeight = getheight();
    int dstX = 0;

    for (int iy = 0; iy < picture_height; iy++) {
        for (int ix = 0; ix < picture_width; ix++) {
            int srcX = ix + iy * picture_width;
            int sa = ((src[srcX] & 0xff000000) >> 24);
            int sr = ((src[srcX] & 0xff0000) >> 16);
            int sg = ((src[srcX] & 0xff00) >> 8);
            int sb = src[srcX] & 0xff;

            int targetX = ix + picture_x;
            int targetY = iy + picture_y;

            if (targetX >= 0 && targetX < graphWidth && targetY >= 0 && targetY < graphHeight) {
                dstX = targetX + targetY * graphWidth;
                if (dstX < graphWidth * graphHeight) {
                    int dr = ((dst[dstX] & 0xff0000) >> 16);
                    int dg = ((dst[dstX] & 0xff00) >> 8);
                    int db = dst[dstX] & 0xff;

                    int r = (sr * sa / 255 + dr * (255 - sa) / 255);
                    int g = (sg * sa / 255 + dg * (255 - sa) / 255);
                    int b = (sb * sa / 255 + db * (255 - sa) / 255);

                    draw[dstX] = (r << 16) | (g << 8) | b;
                }
            }
        }
    }
}

void updateEnemies() {
    for (int i = 0; i < MAX_ENEMIES; i++) {
        if (enemies[i].alive) {
            enemies[i].y += enemies[i].speed;
            if (enemies[i].y > HEIGHT) {
                enemies[i].alive = false;
            }
        }
    }
    spawnEnemy();

    // 生成道具（低频率生成）
    spawnPowerUp();
}

void spawnEnemy() {
    if (enemySpawnTimer > 0) {
        enemySpawnTimer--;
        return;
    }

    for (int i = 0; i < MAX_ENEMIES; i++) {
        if (!enemies[i].alive) {
            enemies[i].type = rand() % 3;

            switch (enemies[i].type) {
            case ENEMY_SMALL:
                enemies[i].width = 57;
                enemies[i].height = 43;
                enemies[i].hp = 1;
                enemies[i].speed = 3 + rand() % 3;
                break;
            case ENEMY_MIDDLE:
                enemies[i].width = 69;
                enemies[i].height = 99;
                enemies[i].hp = 2;
                enemies[i].speed = 2 + rand() % 3;
                break;
            case ENEMY_BIG:
                enemies[i].width = 169;
                enemies[i].height = 258;
                enemies[i].hp = 3;
                enemies[i].speed = 1 + rand() % 3;
                break;
            }
            enemies[i].x = rand() % (WIDTH - enemies[i].width);
            enemies[i].y = -enemies[i].height;
            enemies[i].speed = 1 + rand() % 2;
            enemies[i].alive = true;
            enemySpawnTimer = 30 + rand() % 30;
            break;
        }
    }
}

// 生成道具
void spawnPowerUp() {
    if (powerUpSpawnTimer > 0) {
        powerUpSpawnTimer--;
        return;
    }

    // 固定每250帧(约5秒)生成一个道具
    for (int i = 0; i < powerUps.size(); i++) {
        if (!powerUps[i].active) {
            powerUps[i].active = true;
            powerUps[i].type = (rand() % 2) + 1; // 1:炸弹 2:子弹
            powerUps[i].x = rand() % (WIDTH - powerUps[i].width);
            powerUps[i].y = -powerUps[i].height;
            powerUpSpawnTimer = 250; // 固定5秒刷新(250帧)
            break;
        }
    }
}

void initEnemies() {
    for (int i = 0; i < MAX_ENEMIES; i++) {
        enemies[i].alive = false;
        enemies[i].x = -100;
        enemies[i].y = -100;
        enemies[i].speed = 0;
        enemies[i].type = ENEMY_SMALL;
        enemies[i].hp = 0;
        enemies[i].width = 0;
        enemies[i].height = 0;
    }
}

void checkCollisions() {
    for (int i = 0; i < MAX_BULLETS; i++) {
        if (bullets[i].alive && !playerInvincible) { // 无敌状态下子弹正常飞行
            for (int j = 0; j < MAX_ENEMIES; j++) {
                if (enemies[j].alive) {
                    if (bullets[i].x < enemies[j].x + enemies[j].width &&
                        bullets[i].x + bulletWidth > enemies[j].x &&
                        bullets[i].y < enemies[j].y + enemies[j].height &&
                        bullets[i].y + bulletHeight > enemies[j].y) {

                        bullets[i].alive = false;
                        enemies[j].hp--;

                        if (enemies[j].hp <= 0) {
                            spawnExplosion(
                                enemies[j].x + enemies[j].width / 2,
                                enemies[j].y + enemies[j].height / 2,
                                enemies[j].type);

                            enemies[j].alive = false;
                            int enemyScore = 0;
                            switch (enemies[j].type) {
                            case ENEMY_SMALL: enemyScore = 10; break;
                            case ENEMY_MIDDLE: enemyScore = 20; break;
                            case ENEMY_BIG: enemyScore = 30; break;
                            }
                            score += enemyScore;

                            // 检查是否增加生命
                            currentScoreForLife += enemyScore;
                            checkAddLife();
                        }
                    }
                }
            }
        }
    }

    // 玩家与道具碰撞检测
    for (int i = 0; i < powerUps.size(); i++) {
        if (powerUps[i].active) {
            if (playerX < powerUps[i].x + powerUps[i].width &&
                playerX + playerWidth > powerUps[i].x &&
                playerY < powerUps[i].y + powerUps[i].height &&
                playerY + playerHeight > powerUps[i].y) {

                powerUps[i].active = false; // 道具被拾取

                // 应用道具效果
                if (powerUps[i].type == POWER_UP_BOMB) {
                    bombEffectActive = true;
                    bombEffectTimer = 300; // 10秒效果 (300帧)
                    printf("获得炸弹补给，攻击加速10秒!\n");
                }
                else if (powerUps[i].type == POWER_UP_BULLET) {
                    if (bulletColumns < maxBulletColumns) {
                        bulletColumns++;
                        bulletEffectTimer = 1200; // 40秒效果 (1200帧)
                        printf("获得子弹补给，子弹列数+1!\n");
                    }
                }
            }
        }
    }
}

void spawnExplosion(int x, int y, int type) {
    for (int i = 0; i < MAX_EXPLOSIONS; i++) {
        if (!explosions[i].active) {
            explosions[i].x = x;
            explosions[i].y = y;
            explosions[i].type = type;
            explosions[i].frame = 0;
            explosions[i].active = true;
            return;
        }
    }
}

void updateExplosions() {
    for (int i = 0; i < MAX_EXPLOSIONS; i++) {
        if (explosions[i].active) {
            explosions[i].frame++;
            if (explosions[i].frame > 15) {
                explosions[i].active = false;
            }
        }
    }
}

void checkPlayerHit() {
    if (playerDying || gameOver || playerInvincible) return; // 死亡、游戏结束或无敌状态不检测碰撞

    for (int i = 0; i < MAX_ENEMIES; i++) {
        if (enemies[i].alive) {
            if (playerX < enemies[i].x + enemies[i].width &&
                playerX + playerWidth > enemies[i].x &&
                playerY < enemies[i].y + enemies[i].height &&
                playerY + playerHeight > enemies[i].y) {

                playerDying = true;
                spawnExplosion(
                    playerX + playerWidth / 2,
                    playerY + playerHeight / 2,
                    3);

                lives--; // 减少一条生命
                if (lives <= 0) {
                    gameOver = true; // 所有生命用完，游戏结束
                }

                return;
            }
        }
    }
}

// 检查是否增加生命
void checkAddLife() {
    if (lives < maxLives && currentScoreForLife >= scoreToAddLife) {
        lives++;
        currentScoreForLife -= scoreToAddLife;
        lifeAdded = true;
        lifeAddedTimer = 0;
        printf("获得额外生命！当前生命: %d\n", lives);
    }
}

// 显示生命增加提示
void showLifeAddedMessage() {
    if (lifeAdded) {
        settextcolor(GREEN);
        outtextxy(WIDTH / 2 - 40, HEIGHT / 2 - 50, "获得额外生命!");
        settextcolor(WHITE);

        lifeAddedTimer++;
        if (lifeAddedTimer > 60) {
            lifeAdded = false;
            lifeAddedTimer = 0;
        }
    }
}